package com.hzmg.akkacloud.worker.actors;

import akka.actor.AbstractActor;
import com.hzmg.akkacloud.worker.common.utils.AkkaUtils;
import com.hzmg.akkacloud.worker.common.utils.RemoteInvokeUtils;
import com.hzmg.akkacloud.worker.pojo.RouterInfo;

/**
 * 负责创建路由的actor
 *
 * @author zbw
 * @since 2022/9/17
 */
public class RouterCreatActor extends AbstractActor {

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(RouterInfo.class, this::onReceiveActorNameCreateRouter)
                .build();
    }

    private void onReceiveActorNameCreateRouter(RouterInfo routerInfo) {
        RemoteInvokeUtils.getRouters().computeIfAbsent(routerInfo.getActorName(), ignore -> {
            int totalInstances = routerInfo.getTotalInstances();
            try {
                return AkkaUtils.createrRouter(getContext(), routerInfo.getActorName(), routerInfo.getUserRoleList(), totalInstances, 0);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        });
        getSender().tell("ok",getSelf());
    }
}
